package com.example.admin.controller.candidates;

import com.example.airobotinterviewrecord.util.Result;
import com.example.candiates.domain.InterviewCandidates;
import com.example.candiates.service.CandidateService;
import com.example.candiates.service.InterviewCandidatesService;
import com.example.airobotsettings.util.JwtUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/candidate")
public class CandidateLoginController {
    
    @Autowired
    private CandidateService candidateService;
    
    @Autowired
    private InterviewCandidatesService interviewCandidatesService;
    
    @Autowired
    private JwtUtil jwtUtil;
    
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest request) {
        try {
            // 验证手机号
            if (request.getPhone() == null || request.getPhone().trim().isEmpty()) {
                return Result.error("手机号不能为空");
            }
            
            // 根据手机号查询候选人
            List<InterviewCandidates> candidates = candidateService.getByPhone(request.getPhone());
            if (candidates == null || candidates.isEmpty()) {
                return Result.error("该手机号未注册");
            }
            
            if (candidates.size() > 1) {
                return Result.error("该手机号存在多条记录，请联系管理员处理");
            }
            
            InterviewCandidates candidate = candidates.get(0);
            
            // 生成token
            String token = jwtUtil.generateToken(candidate.getPhone());
            
            // 使用getList接口获取完整的候选人信息
            InterviewCandidates query = new InterviewCandidates();
            query.setPhone(candidate.getPhone());
            Page<InterviewCandidates> page = interviewCandidatesService.selectInterviewCandidatesList(query, 1, 1);
            if (page == null || page.getRecords().isEmpty()) {
                return Result.error("获取候选人信息失败");
            }
            
            // 构建返回数据
            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("candidate", page.getRecords().get(0));
            
            return Result.success(data);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    @GetMapping("/schedule/{candidateId}")
    public Result getInterviewSchedule(@PathVariable Integer candidateId) {
        try {
            if (candidateId == null) {
                return Result.error("候选人ID不能为空");
            }
            
            System.out.println("开始获取面试安排，候选人ID: " + candidateId);
            InterviewCandidates schedule = candidateService.getInterviewSchedule(candidateId);
            
            if (schedule == null) {
                System.out.println("未找到面试安排，候选人ID: " + candidateId);
                return Result.error("未找到面试安排");
            }
            
            System.out.println("成功获取面试安排: " + schedule);
            return Result.success(schedule);
        } catch (Exception e) {
            System.err.println("获取面试安排失败: " + e.getMessage());
            e.printStackTrace();
            return Result.error("获取面试安排失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/interview/{candidateId}")
    public Result getInterviewDetails(@PathVariable Integer candidateId) {
        try {
            if (candidateId == null) {
                return Result.error("候选人ID不能为空");
            }
            
            System.out.println("开始获取面试详情，候选人ID: " + candidateId);
            Map<String, Object> details = candidateService.getInterviewDetails(candidateId);
            
            if (details.isEmpty()) {
                System.out.println("未找到面试详情，候选人ID: " + candidateId);
                return Result.error("未找到面试详情");
            }
            
            System.out.println("成功获取面试详情: " + details);
            return Result.success(details);
        } catch (Exception e) {
            System.err.println("获取面试详情失败: " + e.getMessage());
            e.printStackTrace();
            return Result.error("获取面试详情失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/interview/start/{candidateId}")
    public Result startInterview(@PathVariable Integer candidateId) {
        try {
            if (candidateId == null) {
                return Result.error("候选人ID不能为空");
            }
            
            System.out.println("开始面试，候选人ID: " + candidateId);
            boolean success = candidateService.startInterview(candidateId);
            
            if (!success) {
                System.out.println("开始面试失败，候选人ID: " + candidateId);
                return Result.error("开始面试失败");
            }
            
            System.out.println("成功开始面试，候选人ID: " + candidateId);
            return Result.success();
        } catch (Exception e) {
            System.err.println("开始面试失败: " + e.getMessage());
            e.printStackTrace();
            return Result.error("开始面试失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/interview/complete/{candidateId}")
    public Result completeInterview(@PathVariable Integer candidateId) {
        try {
            if (candidateId == null) {
                return Result.error("候选人ID不能为空");
            }
            
            System.out.println("完成面试，候选人ID: " + candidateId);
            boolean success = candidateService.completeInterview(candidateId);
            
            if (!success) {
                System.out.println("完成面试失败，候选人ID: " + candidateId);
                return Result.error("完成面试失败");
            }
            
            System.out.println("成功完成面试，候选人ID: " + candidateId);
            return Result.success();
        } catch (Exception e) {
            System.err.println("完成面试失败: " + e.getMessage());
            e.printStackTrace();
            return Result.error("完成面试失败: " + e.getMessage());
        }
    }
    
    public static class LoginRequest {
        private String phone;
        private String password;
        
        public String getPhone() {
            return phone;
        }
        
        public void setPhone(String phone) {
            this.phone = phone;
        }
        
        public String getPassword() {
            return password;
        }
        
        public void setPassword(String password) {
            this.password = password;
        }
    }
} 